/*******************************************************************************
 * ShopSettingDelegator.java
 *
 * Created by Nishan
 * Last modified Date: Sep 9, 2008 2:25:00 PM by ankur
  * Version : 0.1
 * Copyright (c) 2008 - 2009 cyberThink Infotech Pvt. Ltd., All rights reserved.
 ******************************************************************************/

package com.printeverywhere.delegator;

import java.io.File;
import java.io.IOException;
import java.util.List;
import static com.printeverywhere.util.AttributeConstants.*;

import org.apache.commons.io.FileUtils;
import org.apache.log4j.Logger;
import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Restrictions;
import com.printeverywhere.dto.ShopSettings;
import com.printeverywhere.dto.Template;
import com.printeverywhere.dto.User;
import com.printeverywhere.util.AttributeConstants;
import com.printeverywhere.util.HibernateUtil;

/**
 * The Class ShopSettingDelegator.
 */
public class ShopSettingDelegator {
	
	/** The log. */
	private static Logger log = Logger.getLogger(ShopSettingDelegator.class);
	
	/**
	 * Save shop settings.
	 * 
	 * @param shopSettings the shop settings
	 * 
	 * @throws HibernateException the hibernate exception
	 */
	public static void saveShopSettings(ShopSettings shopSettings) throws HibernateException {
		if(log.isInfoEnabled()){
			log.info("Called saveShopSettings method".intern());
		}
		Session session = HibernateUtil.getSession();
		Transaction tx = null;
		try {
			tx = session.beginTransaction();
			session.saveOrUpdate(shopSettings);
			tx.commit();
		} catch (RuntimeException e) {
			log.error("Got exception when Save Shop Settings".intern());
			if (tx != null)
				tx.rollback();
			throw e;
		} finally {
			if(log.isInfoEnabled()){
				log.info(CLOSING_SESSION);
			}
			session.flush();
			session.close();
		}
	}

	/**
	 * Gets the shop settings.
	 * 
	 * @param userId the user id
	 * 
	 * @return the shop settings
	 * 
	 * @throws HibernateException the hibernate exception
	 */
	@SuppressWarnings("unchecked")
	public static ShopSettings getShopSettings(int userId) throws HibernateException {
		if(log.isInfoEnabled()){
			log.info("Getting list for Shop Settings by Id".intern());
		}
		Session session = HibernateUtil.getSession();
		try{
			session.beginTransaction();
			Criteria criteria = session.createCriteria(ShopSettings.class);
			criteria.add(Restrictions.eq(AttributeConstants.USER , new User(userId)));
			List<ShopSettings> shopsettingslist = criteria.list();
			return ((shopsettingslist.size() > 0) ? shopsettingslist.get(0) : null);
		}catch (HibernateException e) {
			log.error("Got exception when getting list for Shop Settings by Id".intern());
			throw e;
		} finally {
			if(log.isInfoEnabled()){
				log.info(CLOSING_SESSION);
			}
			session.flush();
			HibernateUtil.closeSession();
		}
	}
	
	public static List<ShopSettings> getShopSettingsList() throws HibernateException {
		if(log.isInfoEnabled()){
			log.info("Getting list for Template ".intern());
		}
		Session session = HibernateUtil.getSession();
		try {
			Transaction transaction = session.beginTransaction();
			List<ShopSettings> list = session.createCriteria(ShopSettings.class).list();
			transaction.commit();
			return list;
		} catch (HibernateException e) {
			log.error("Got exception when getting list for Template ".intern());
			throw e;
		} finally {
			if(log.isInfoEnabled()){
				log.info(CLOSING_SESSION);
			}
			session.flush();
			session.close();
		}
	}
	
	
	
	
}
